﻿using famails.bus;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails
{
    public partial class tintuc : System.Web.UI.Page
    {
        static public ArticleBUS bus = new ArticleBUS();
        public GroupBUS bus_group = new GroupBUS ();
        static object id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable topRead = bus.GetTop1Read();
                if (topRead != null && topRead.Rows.Count > 0)
                {
                    imgTopRead.ImageUrl = topRead.Rows[0]["image"] + "";
                    lblTopRead.Text = topRead.Rows[0]["title"] + "";
                    lblTopRead1.NavigateUrl = lblTopRead.NavigateUrl = "tintuc_detail.aspx?id=" + topRead.Rows[0]["articleid"];
                }

                DataTable newest = bus.GetNewest();
                if (newest != null && newest.Rows.Count > 0)
                {
                    id = newest.Rows[0]["articleid"];
                    imgNewest.ImageUrl = newest.Rows[0]["image"] + "";
                    lblNewest_Title.Text = newest.Rows[0]["title"] + "";
                    lblNewest_Sum.Text = newest.Rows[0]["summary"] + "";
                    int temp = 0;
                    int.TryParse(newest.Rows[0]["viewcount"] + "",out temp);
                    lblViewCount.Text = temp.ToString("#,##");

                    hplDetail.NavigateUrl = hplDetail1.NavigateUrl = hplDetail2.NavigateUrl = "tintuc_detail.aspx?id=" + newest.Rows[0]["articleid"];
                }
                rptCategory.DataSource = new CategoryBUS().GetAll("", 1);
                rptCategory.DataBind();
            }
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<ArticleDTO> LoadNews(int groupid, string keyword)
        {
            DataTable source = bus.Search(keyword,1,-1,groupid);
            List<ArticleDTO> result = new List<ArticleDTO>();
            ArticleDTO item = new ArticleDTO();
            foreach (DataRow dr in source.Rows)
            {
                if (dr["ArticleID"] + "" != id + "")
                {
                    item = new ArticleDTO();
                    item.ArticleID = Convert.ToInt32(dr["ArticleID"] + "");
                    item.ViewCount = Convert.ToInt32(dr["Viewcount"] + "");
                    item.Title = dr["Title"] + "";
                    item.Summary = dr["Summary"] + "";
                    item.CategoryName = dr["CategoryName"] + "";
                    item.GroupName = dr["GroupName"] + "";
                    item.Show = dr["Show"] + "";
                    item.Image = dr["iMAGE"] + "";
                    result.Add(item);
                }
            }

            return result;
        }
    }
}